package com.pro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pro.domain.Cinema;
import com.pro.domain.Details;
import com.pro.domain.Film;
import com.pro.domain.TheaterRoom;
import com.pro.mapper.CinemaMapper;
import com.pro.mapper.DetailsMapper;
import com.pro.mapper.FilmMapper;
import com.pro.mapper.TheaterRoomMapper;
import com.pro.service.DetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DetailsServiceImpl implements DetailsService {

    @Autowired
    private DetailsMapper detailsMapper;

    @Autowired
    private FilmMapper filmMapper;

    @Autowired
    private TheaterRoomMapper theaterRoomMapper;

    @Autowired
    private CinemaMapper cinemaMapper;

    @Override
    public List<Cinema> queryCinemaToFilm(Long filmId) {
        return detailsMapper.queryCinemaToFilm(filmId);
    }

    @Override
    public List<Details> queryDetailsByFilmId(Long filmId) {
        //1.查出对应电影的拍片
        QueryWrapper<Details> detailsQueryWrapper = new QueryWrapper<Details>();
        detailsQueryWrapper.eq("film_id", filmId);
        List<Details> details = detailsMapper.selectList(detailsQueryWrapper);

        //2.放入每一条记录对应的电影、电影院、播放厅
        for (Details detail : details) {
            //电影
            setDetailsClass(detail);
        }

        return details;
    }

    @Override
    public List<Details> queryFilmSpecificDetails(Long filmId, Long cinemaId) {
        QueryWrapper<Details> queryWrapper = new QueryWrapper<Details>();
        queryWrapper.eq("film_id", filmId);
        queryWrapper.eq("cinema_id", cinemaId);
        List<Details> details = detailsMapper.selectList(queryWrapper);
        //电影
        for (Details detail : details) {
            setDetailsClass(detail);
        }
        return details;
    }

    /**
     * 给Details类的属性(Film,Cinema,TheaterRoom)赋值
     */
    private void setDetailsClass(Details detail) {
        QueryWrapper<Film> filmQueryWrapper = new QueryWrapper<Film>();
        filmQueryWrapper.eq("film_id", detail.getFilmId());
        detail.setFilm(filmMapper.selectOne(filmQueryWrapper));

        //电影院
        QueryWrapper<Cinema> cinemaQueryWrapper = new QueryWrapper<Cinema>();
        cinemaQueryWrapper.eq("cinema_id", detail.getCinemaId());
        detail.setCinema(cinemaMapper.selectOne(cinemaQueryWrapper));

        //播放厅
        QueryWrapper<TheaterRoom> theaterRoomQueryWrapper = new QueryWrapper<TheaterRoom>();
        theaterRoomQueryWrapper.eq("tr_id", detail.getTrId());
        detail.setTheaterRoom(theaterRoomMapper.selectOne(theaterRoomQueryWrapper));
    }
}
